package engine;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import tower.BigCannon;
import tower.MachineAntiArmor;
import tower.MachineGun30mm;
import tower.SmallCannon;
import tower.V2Launcher;

import button.Button;
import button.PressedButton;
import button.StandardButton;

import constans.Flag;
import constans.ResourceManager;
import controller.ObjectController;
import describe.BuildingDescribe;
import describe.Describe;
import describe.MyBuildingDescribe;
import describe.UnitDescribe;
import entity.Building;
import entity.Entity;

public class Frame {

	private Image frame;
	private Image money;
	private MiniMap miniMap;
	private Button[] tower;
	
	private int flag;
	private int towerFlag;
	
	private Describe describe;
	private boolean drawDescribe;
	
	public Frame()
	{
		miniMap = new MiniMap();
		frame = ResourceManager.getImage("frame");
		money = ResourceManager.getImage("money");
		tower = new Button[4];
		
		tower[0] = new PressedButton(625,430,50,50,Flag.BUILD,ResourceManager.getImage("tmp"),ResourceManager.getSound("click"));
		tower[1] = new PressedButton(680,430,50,50,Flag.BUILD,ResourceManager.getImage("tmp"),ResourceManager.getSound("click"));
		tower[2] = new PressedButton(735,430,50,50,Flag.BUILD,ResourceManager.getImage("tmp"),ResourceManager.getSound("click"));
		tower[3] = new PressedButton(625,490,50,50,Flag.BUILD,ResourceManager.getImage("b_v2"),ResourceManager.getSound("click"));
		//tower[4] = new PressedButton(680,490,50,50,Flag.BUILD,ResourceManager.getImage("b_v2"),ResourceManager.getSound("click"));
		//tower[5] = new PressedButton(735,490,50,50,Flag.BUILD,ResourceManager.getImage("b_v2"),ResourceManager.getSound("click"));
		
	}
	public void render(GameContainer container, Graphics g) throws SlickException
	{	
		frame.draw(0,0);
		for(Button t : tower)
			t.render(container, g);
		miniMap.render(container, g);
		//money
		g.setColor(new Color(0,57,0));		
		g.fillRoundRect(625, 545, 165, 40, 10);
		g.setColor(Color.yellow);

		money.draw(657, 547);
		g.drawString(""+ObjectController.player.getMoney(), 720, 557);
		if(drawDescribe)
			describe.render(container, g);
	}
	public void update(GameContainer container, int delta) throws SlickException
	{
		Input input = container.getInput();
		
		miniMap.update(container, delta);
		if(drawDescribe)
		{
			describe.update(container, delta);
			if(describe.getFlag() == Flag.SELL)
				drawDescribe = false;
		}
			
		for(int i = 0 ; i < tower.length ; i++)
		{		
			tower[i].hover(input);
			flag = 	tower[i].click(input);
			if(tower[i].wasClicked())
			{		
				towerFlag = Flag.BUILD + i + 1;
				switchBuilding(towerFlag);
				deClick(i);
				return;
			}
		}
	}
	public int getTowerFlag()
	{
		return towerFlag;
	}
	public int getFlag()
	{
		return flag;
	}
	public void deClick(int j)
	{
			for(int i = 0 ; i < tower.length ; i++)
				if(i != j)
					tower[i].deClick();
	}
	private void switchBuilding(int t)
	{
		Building b = null;
		switch(t)
		{
		case Flag.SMALL_CANNON:
			b = new SmallCannon(-100,-100);
			break;
		case Flag.BIG_CANNON:
			b = new BigCannon(-100,-100);
			break;	
		case Flag.MACHINE_GUN:
			b = new MachineGun30mm(-100,-100);
			break;	
		case Flag.V2LAUNCHER:
			b = new V2Launcher(-100,-100);
			break;		
		case Flag.MACHINE_AA:
			b = new MachineAntiArmor(-100,-100);
			break;		
		}
		if(b != null)
			describe = new BuildingDescribe(b);
	}
	public void setDrawDescribe(boolean b)
	{
		drawDescribe = b;
	}
	public void setDrawDescribe(Entity b)
	{
		if(b != null)
		{
			if(b.getType() == Entity.BUILDING)
			{
				drawDescribe = true;
				describe = new MyBuildingDescribe(b);
			}
			if(b.getType() == Entity.UNIT)
			{
				drawDescribe = true;
				describe = new UnitDescribe(b);
			}
		}
		else
			drawDescribe = false;
	}

		
}
